<template>
    <div class="container-common manage-course-resource">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>课程信息管理</el-breadcrumb-item>
            <el-breadcrumb-item>课程资源</el-breadcrumb-item>
        </el-breadcrumb>
        <el-upload
                class="upload-container"
                action="http://39.105.144.7:5002/api/upload"
                :data="otherUploadData"
                name="file"
                :file-list="fileList"
                :show-file-list="false"
                :on-success="handleSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
            <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
        </el-upload>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    prop="filename"
                    label="文件名">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="300">
                <template slot-scope="scope">
                    <el-button
                            @click="handleDownload(scope.row)"
                            type="text"
                            size="large">下载</el-button>
                    <el-button
                            type="text"
                            size="large"
                            @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                title="提示"
                :visible.sync="deleteDialogVisible"
                width="30%">
            <span v-if="currentData">确认删除资源{{currentData.filename}}吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelDelete">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

    import Api from '@utils/api';
    import { mapGetters } from 'vuex';

    export default {
        name: "resource",
        data () {
            return {
                tableData: [],
                fileList: [],
                deleteDialogVisible: false,
                currentData: null
            }
        },
        computed: {
            ...mapGetters([
                'getCurrentCourseId'
            ]),
            otherUploadData () {
                return {
                    type: 'course',
                    id: this.getCurrentCourseId
                }
            }
        },
        methods: {
            handleDownload (data) {
                window.open('http://39.105.144.7:5002/api/download?UUID=' + data.id);
            },
            cancelDelete () {
                this.deleteDialogVisible = false;
            },
            confirmDelete () {
                Api({
                    method: 'post',
                    url: '/deleteResource',
                    data: {
                        id: this.currentData.id
                    }
                }).then(() => {
                    this.$notify({
                        title: "删除成功！",
                        type: "success"
                    });

                    this.cancelDelete();
                    this.getResourceList();
                })
            },
            handleDelete (data) {
                this.currentData = data;
                this.deleteDialogVisible = true;
            },
            getResourceList () {
                Api({
                    method: 'post',
                    url: '/getResourceList',
                    data: {
                        type: 'course',
                        id: this.getCurrentCourseId
                    }
                }).then(res => {
                    if (res.data && res.data.data && res.data.data.length) {
                        this.tableData = res.data.data.map(item => {
                            return {
                                id: item.id,
                                filename: item.filename
                            }
                        });
                    }
                })
            },
            handleSuccess () {
                this.fileList = [];
                this.getResourceList();
            }
        },
        created () {
            this.getResourceList();
        }
    }
</script>

<style type="text/scss" lang="scss">
    .manage-course-resource {
        .upload-container {
            margin-bottom: 20px;
        }
    }
</style>